package com.blogs.service;

import java.util.Map;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.entity.UserCenterInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetUserCenter {
	public interface IMsgCallBack {
		public void onSuccess(UserCenterInfo author);

		public void onFailed();
	}

	private String url = "";
	private GetMsgTask timeLine;
	private IMsgCallBack callback;

	public GetUserCenter(String sid, String uid, IMsgCallBack callback) {
		// TODO Auto-generated constructor stub
		this.callback = callback;
		timeLine = new GetMsgTask(taskCallBack);
		this.url=AppConfig.GET_USER_CENTER;
		this.url=this.url.replace("{0}", sid).replace("{1}", uid);
	}

	public void loadData() {
		timeLine.execute(url);
	}

	private GetMsgTask.onTaskCallBack taskCallBack = new GetMsgTask.onTaskCallBack() {
		public void onSuccess(String result) {
    		GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<Map<String, Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			UserCenterInfo uci=new UserCenterInfo(msg.data);
			callback.onSuccess(uci);
		};

		public void onFailed() {
			callback.onFailed();
		};
	};
}
